package com.example.myandroidtest.ui.image;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.example.myandroidtest.R;
import com.example.myandroidtest.databinding.ActivityImageBinding;
import com.squareup.picasso.Picasso;

public class ImageActivity extends AppCompatActivity {

    String imageUrl1 = "https://pics0.baidu.com/feed/aa18972bd40735fa9cd3d6868c2aa6bd0f240873.jpeg";
    String imageUrl2 = "";
    String imageUrl3 = "";

    ActivityImageBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityImageBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        initView();
    }

    private void initView() {
        binding.toolbar.ivBack.setOnClickListener(v -> finish());
        binding.toolbar.tvTitle.setText("图片加载");

        binding.btnRequest.setOnClickListener(v -> {
            System.out.println("onClick: ");
            glideTest();
            picassoTest();
        });


        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            // 权限未被授予，请求权限
            ActivityCompat.requestPermissions(this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 11);
        } else {
            // 权限已被授予，执行相关操作
        }
    }


    void glideTest() {
        Glide.with(this).load(imageUrl1).placeholder(R.mipmap.ic_launcher).into(binding.ivGlide);
    }

    void picassoTest() {
        Picasso.with(this).load(imageUrl1).placeholder(R.mipmap.ic_launcher).into(binding.ivPicasso);
    }
}